package br.cin.stadium.middleware.network;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import br.cin.stadium.middleware.util.Logger;

public class BufferStream {
	
	OutputStream outStream;
	InputStream inStream;
	
	public BufferStream(OutputStream os){
		this.outStream = os;
	}

	public BufferStream(InputStream is){
		this.inStream = is;
	}
	
	public int readByte() {
		int b = -1;
		
		if(inStream != null) {
			try {
				b = inStream.read();
			} catch (IOException ioe) {
				Logger.logError("BufferStream", "readByte", "Error", ioe);
			}
		}
		return b;
	}
	
	public byte[] readBytes(int size) {
		byte[] data = new byte[size];
		
		if(inStream != null) {
			try {
				inStream.read(data, 0, size);
			} catch (IOException ioe) {
				Logger.logError("BufferStream", "readBytes", "Error", ioe);
			}
		}
		return data;
	}

	
	public void writeByte(byte b) {
		if(outStream != null) {
			try {
				outStream.write(b);
			} catch (IOException ioe) {
				Logger.logError("BufferStream", "writeByte", "Error", ioe);
			}
		}
	}

	public void writeBytes(byte[] b) {
		if(outStream != null) {
			try {
				outStream.write(b);
			} catch (IOException ioe) {
				Logger.logError("BufferStream", "writeBytes", "Error", ioe);
			}
		}
	}

}
